package com.huang;

import com.huangwei.entity.RpcRequest;
import com.huangwei.entity.RpcResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.Socket;

/**
 * 实际进行过程调用的工作线程
 */
public class WorkerThread implements Runnable{

    private static final Logger logger = LoggerFactory.getLogger(WorkerThread.class);

    private Socket socket;
    private Object service;

    public WorkerThread(Socket socket, Object service) {
        this.socket = socket;
        this.service = service;
    }

    @Override
    public void run() {
        try(ObjectInputStream objectInputStream = new ObjectInputStream(socket.getInputStream());
            ObjectOutputStream objectOutputStream = new ObjectOutputStream(socket.getOutputStream())){
            RpcRequest rpcRequest = (RpcRequest) objectInputStream.readObject();
            Method method = service.getClass().getMethod(rpcRequest.getMethodName(),rpcRequest.getParamTypes());
            Object returnObject = method.invoke(service,rpcRequest.getParameters());
            objectOutputStream.writeObject(RpcResponse.success(returnObject,"123"));
//            logger.info(RpcResponse.);
            objectOutputStream.flush();
            logger.info(rpcRequest.getInterfaceName()+" "+rpcRequest.getMethodName()+" "+rpcRequest.getParameters()+" "+ rpcRequest.getParamTypes());
        } catch (IOException | ClassNotFoundException | NoSuchMethodException | IllegalAccessException | InvocationTargetException e){
            logger.error("调用或发送时：" , e);
        }
    }
}
